package com.yiyi.credit.manage.abutment.provider;

import com.yiyi.credit.manage.model.entity.ProjectInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;


public class ProjectInfoSqlProvider {

    public String getProjectInfoPageList(Map<String, Object> paramMap) {
        return new SQL() {{
            SELECT("id,regionCode,name,accountNumber,pwd,privateKey,remark,ipAddress,linkMan,linkManPhone,status,createTime,createPeople,updateTime,updatePeople");
            FROM("project_info");
            WHERE(buildWhereSql(paramMap));
        }}.toString();
    }

    private String buildWhereSql(Map<String, Object> paramMap) {

        ProjectInfo projectInfo = (ProjectInfo) paramMap.get("projectInfo");
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" 1=1 ");
        if (StringUtils.isNotBlank(projectInfo.getName())) {
            sqlBuilder.append(" AND name = #{projectInfo.name}");
        }
        if (StringUtils.isNotBlank(projectInfo.getLinkMan())) {
            sqlBuilder.append(" AND linkMan = #{projectInfo.linkMan}");
        }
        if (StringUtils.isNotBlank(projectInfo.getLinkManPhone())) {
            sqlBuilder.append(" AND linkManPhone = #{projectInfo.linkManPhone} ");
        }
        if (StringUtils.isNotBlank(String.valueOf(projectInfo.getStatus()))) {
            if (projectInfo.getStatus() != -1) {
                sqlBuilder.append(" AND status = #{projectInfo.status} ");
            }
        }

        return sqlBuilder.toString();

    }

}